package com.jie.eduservice.controller.front;

import com.jie.commonutils.JwtUtils;
import com.jie.commonutils.ResultEntity;
import com.jie.commonutils.ordervo.CourseWebVOOrder;
import com.jie.eduservice.client.OrderClient;
import com.jie.eduservice.entity.chapter.ChapterVO;
import com.jie.eduservice.entity.vo.front.CourseFrontVO;
import com.jie.eduservice.entity.vo.front.CourseWebVO;
import com.jie.eduservice.service.EduChapterService;
import com.jie.eduservice.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 前台课程相关的Controller类
 * @author XieZhiJie
 * @create 2020-10-17 12:20
 */
//@CrossOrigin  // 解决跨域问题
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    private final EduCourseService courseService;

    private final EduChapterService chapterService;

    private final OrderClient orderClient;

    @Autowired
    public CourseFrontController(
            EduCourseService courseService,
            EduChapterService chapterService,
            OrderClient orderClient) {
        this.courseService = courseService;
        this.chapterService = chapterService;
        this.orderClient = orderClient;
    }

    /**
     * 1.条件查询带分页查询课程
     * @param page           当前页(之前用的current, 现在改了)
     * @param limit          每页记录数
     * @param courseFrontVO  封装查询信息
     * @return               返回查询到的课程数据
     */
    @PostMapping("/getFrontCourseList/{page}/{limit}")
    public ResultEntity getFrontCourseList(
            @PathVariable("page") Long page,
            @PathVariable("limit") Long limit,
            @RequestBody(required = false) CourseFrontVO courseFrontVO
            ) {
        Map<String, Object> map = courseService.getCourseFrontList(page, limit, courseFrontVO);
        return ResultEntity.ok().data(map);
    }

    /**
     * 2.根据课程id查询课程详情的方法
     * @param courseId  要查询的课程详细信息的课程id
     * @param request   从request中获取token
     * @return          返回查询到的信息
     */
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public ResultEntity getFrontCourseInfo(
            @PathVariable("courseId") String courseId,
            HttpServletRequest request
    ) {
        // 1.根据课程id, 查询课程信息
        CourseWebVO courseWebVO = courseService.getBaseCourseInfo(courseId);

        // 2.根据课程id, 查询出课程大纲列表
        List<ChapterVO> chapterVideoList = chapterService.getChapterVideoByCourseId(courseId);

        // 3.根据课程id和用户id查询当前课程是否已经支付过了
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse = false;
        if (!StringUtils.isEmpty(memberId)) {
            buyCourse = orderClient.isBuyCourse(courseId, memberId);
        }

        return ResultEntity.ok().data("courseWebVo", courseWebVO).data("chapterVideoList", chapterVideoList).data("isBuy", buyCourse);
    }

    /**
     * 根据课程id查询课程信息
     * 用于远程调用
     * @param id  课程id
     * @return    返回课程信息
     */
    @PostMapping("/getCourseInfoOrder/{id}")
    public CourseWebVOOrder getCourseInfoOrder(
            @PathVariable("id") String id
    ) {
        CourseWebVO courseWebVO = courseService.getBaseCourseInfo(id);
        CourseWebVOOrder courseWebVOOrder = new CourseWebVOOrder();
        BeanUtils.copyProperties(courseWebVO, courseWebVOOrder);
        return courseWebVOOrder;
    }

}
